package com.jyserver.common.AutoCode.jybaits;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jyserver.common.AutoCode.jybaits.element.pc.FormTableView;
import com.jyserver.common.AutoCode.jybaits.element.pc.From.Creat;
import com.jyserver.common.AutoCode.jybaits.element.pc.Tables.table;
import com.jyserver.common.utils.HelpUtils;
import com.jyserver.common.utils.JyFile;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

/**
 * @Description :根据json文件生成前端代码
 */
public class GeneratorWebByJson {
    /**
     * @Description :获得文件内容
     */
    private static JSONObject jsonObject;
    private static JSONObject jsonObjectSon;
    private static JSONArray jsonArray;

    public void getjson(String Path) throws IOException {
//         jsonObject= JSONObject.parseObject(JyFile.FileInputStream(Path));
        jsonArray = JSONObject.parseArray(JyFile.FileInputStream(Path));
    }

    @Test
    public void createall() {
        try {
            getjson("E:\\myselfProject\\StockServer\\src\\main\\java\\com\\jyserver\\common\\AutoCode\\jybaits\\api_param.json");
        } catch (IOException e) {
            e.printStackTrace();
        }
        int a = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            jsonObject = JSONObject.parseObject(String.valueOf(jsonArray.get(i)));
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                jsonObjectSon = JSONObject.parseObject(String.valueOf(entry.getValue()));
                for (Map.Entry<String, Object> entrySon : jsonObjectSon.entrySet()) {
                    if (!entrySon.getKey().equals("请求参数") && !entrySon.getKey().equals("请求头") && !entrySon.getKey().equals("api")) {
                        JyFile.appendMethod("E:\\myselfProject\\StockWeb\\app\\mobile\\tests\\" + entry.getKey() + ".html", entrySon.getKey() + ":" + String.valueOf(entrySon.getValue()));
                        a = a + 1;
                    }
                }

            }
        }
    }

    public void create(String key1, String key2) {
        try {
            getjson("E:\\myselfProject\\StockServer\\src\\main\\java\\com\\jyserver\\common\\AutoCode\\jybaits\\api_param.json");
        } catch (IOException e) {
            e.printStackTrace();
        }


        for (int i = 0; i < jsonArray.size(); i++) {
            jsonObject = JSONObject.parseObject(String.valueOf(jsonArray.get(i)));
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                jsonObjectSon = JSONObject.parseObject(String.valueOf(entry.getValue()));
                if (entry.getKey().toString().equals(key1) || HelpUtils.isEmpty(key1)) {
                    for (Map.Entry<String, Object> entrySon : jsonObjectSon.entrySet()) {
                        if ((entrySon.getKey().equals(key2) || HelpUtils.isEmpty(key2))&&!"api".equals(entrySon.getKey())) {
                            table creat=new table();
                            String str = creat.test(entrySon.getValue(), entry.getKey(), entrySon.getKey(), jsonObjectSon.get("api"));
                            JyFile.WriteToFile("E:\\project.db\\tl_hospital\\src\\view\\test\\" + entry.getKey()+entrySon.getKey() + ".vue", str);
                        }
                    }
                }

            }
        }
    }


    public void createView(String key1, String key2) {
        try {
            getjson("E:\\myselfProject\\StockServer\\src\\main\\java\\com\\jyserver\\common\\AutoCode\\jybaits\\api_param.json");
        } catch (IOException e) {
            e.printStackTrace();
        }


        for (int i = 0; i < jsonArray.size(); i++) {
            jsonObject = JSONObject.parseObject(String.valueOf(jsonArray.get(i)));
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                jsonObjectSon = JSONObject.parseObject(String.valueOf(entry.getValue()));
                if (entry.getKey().toString().equals(key1) || HelpUtils.isEmpty(key1)) {
                    for (Map.Entry<String, Object> entrySon : jsonObjectSon.entrySet()) {
                        if ((entrySon.getKey().equals(key2) || HelpUtils.isEmpty(key2))&&!"api".equals(entrySon.getKey())) {
                            Creat creat=new Creat();
                            String str = creat.test(entrySon.getValue(), entry.getKey(), entrySon.getKey(), jsonObjectSon.get("api"));
                            JyFile.WriteToFile("E:\\project.db\\tl_hospital\\src\\view\\test\\" + entry.getKey()+entrySon.getKey() + ".vue", str);
                        }
                    }
                }

            }
        }
    }

    public void createForm(String key1, String key2) {
        try {
            getjson("D:\\work\\IDEASPACE\\stock\\src\\main\\java\\com\\jyserver\\common\\AutoCode\\jybaits\\api_param.json");
        } catch (IOException e) {
            e.printStackTrace();
        }


        for (int i = 0; i < jsonArray.size(); i++) {
            jsonObject = JSONObject.parseObject(String.valueOf(jsonArray.get(i)));
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                jsonObjectSon = JSONObject.parseObject(String.valueOf(entry.getValue()));
                if (entry.getKey().toString().equals(key1) || HelpUtils.isEmpty(key1)) {
                    for (Map.Entry<String, Object> entrySon : jsonObjectSon.entrySet()) {
                        if ((entrySon.getKey().equals(key2) || HelpUtils.isEmpty(key2))&&!"api".equals(entrySon.getKey())) {
                            Creat creat=new Creat();
                            String str = creat.test(entrySon.getValue(), entry.getKey(), entrySon.getKey(), jsonObjectSon.get("api"));
                            JyFile.WriteToFile("E:\\project.db\\tl_hospital\\src\\view\\test\\" + entry.getKey()+entrySon.getKey() + ".vue", str);
                        }
                    }
                }

            }
        }
    }

    public void FormTableView(String key1, String key12,String key21, String key22,String key31, String key32,String key41, String key42,String key51, String key52, String note) {
        try {
            getjson("D:\\work\\IDEASPACE\\stock\\src\\main\\java\\com\\jyserver\\common\\AutoCode\\jybaits\\api_param.json");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Object Tbalevue=null;
        Object Viewvue=null;
        Object Creatvue=null;
        String createAPI=null;
        String handleViewAPI=null;
        String handleDeleteAPI=null;
        String updateApi=null;
        String postapi="";
        for (int i = 0; i < jsonArray.size(); i++) {
            jsonObject = JSONObject.parseObject(String.valueOf(jsonArray.get(i)));
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                jsonObjectSon = JSONObject.parseObject(String.valueOf(entry.getValue()));
                if (entry.getKey().toString().equals(key1) || HelpUtils.isEmpty(key1)) {
                    for (Map.Entry<String, Object> entrySon : jsonObjectSon.entrySet()) {
                        if ((entrySon.getKey().equals(key12) || HelpUtils.isEmpty(key12))&&!"api".equals(entrySon.getKey())) {
                            Tbalevue=entrySon.getValue();
                            postapi= String.valueOf(jsonObjectSon.get("api"));
                        }
                    }
                }
                if (entry.getKey().toString().equals(key21) || HelpUtils.isEmpty(key21)) {
                    for (Map.Entry<String, Object> entrySon : jsonObjectSon.entrySet()) {
                        if ((entrySon.getKey().equals(key22) || HelpUtils.isEmpty(key22))&&!"api".equals(entrySon.getKey())) {
                            Viewvue=entrySon.getValue();

                            handleViewAPI= String.valueOf(jsonObjectSon.get("api"));
                        }
                    }
                }
                if (entry.getKey().toString().equals(key31) || HelpUtils.isEmpty(key31)) {
                    for (Map.Entry<String, Object> entrySon : jsonObjectSon.entrySet()) {
                        if ((entrySon.getKey().equals(key32) || HelpUtils.isEmpty(key32))&&!"api".equals(entrySon.getKey())) {
                            createAPI= String.valueOf(jsonObjectSon.get("api"));
                            System.out.println(entrySon.getValue());
                            Creatvue=entrySon.getValue();
                        }
                    }
                }
                if (entry.getKey().toString().equals(key41) || HelpUtils.isEmpty(key41)) {
                    for (Map.Entry<String, Object> entrySon : jsonObjectSon.entrySet()) {
                        if ((entrySon.getKey().equals(key42) || HelpUtils.isEmpty(key42))&&!"api".equals(entrySon.getKey())) {
                            updateApi= String.valueOf(jsonObjectSon.get("api"));
                        }
                    }
                }

                if (entry.getKey().toString().equals(key51) || HelpUtils.isEmpty(key51)) {
                    for (Map.Entry<String, Object> entrySon : jsonObjectSon.entrySet()) {
                        if ((entrySon.getKey().equals(key52) || HelpUtils.isEmpty(key52))&&!"api".equals(entrySon.getKey())) {
                            handleDeleteAPI= String.valueOf(jsonObjectSon.get("api"));
                        }
                    }
                }
            }
        }
        FormTableView f=new FormTableView();
        String str = f.test( Tbalevue,  Viewvue,  Creatvue,  createAPI,  handleViewAPI,  handleDeleteAPI,  updateApi, postapi,  note);
        JyFile.WriteToFile("D:\\work\\developer\\node\\tl_hospital\\src\\view\\usercenter\\" +note + ".vue", str.replace("http://d.huayuboneng.com",""));
    }


    @Test
    public void test() {
        try {
//            create("获取医生报道","返回图书信息说明");
            FormTableView("获取单一就诊人", "就诊人对象说明","获取单一就诊人", "就诊人对象说明","就诊人创建", "请求参数","就诊人更新", "请求参数","删除就诊人","请求参数","就诊模块");

//            FormTableView("获取医生报道", "返回图书信息说明","获取医生话题具体信息", "返回图书信息说明","创建话题", "请求参数","更新话题", "请求参数","删除话题","请求参数");

//            createView("获取医生话题具体信息","返回图书信息说明");
//            createView("获取医生基本信息","医生信息说明");
//            createForm("更新话题","请求参数");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}





